package com.spikything.utils 
{
	import flash.display.Stage;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	
	/**
	 * FlashSize - Manages Flash browser scaling to allow 100% width/height with a minimum width/height
	 * @author Liam O'Donnell
	 * @version 1.0
	 * @usage Simply call the static method FlashSize.setup(minWidth, minHeight);
	 * @see http://www.spikything.com/blog for info/updates
	 * This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
	 * © 2010 spikything.com
	 */
	public class FlashSize
	{
		static private const JAVASCRIPT :String = "var flashTarget;var minWidth;var minHeight;function fitFlash(flashId,w,h){minWidth=w;minHeight=h;flashTarget=document.getElementById(flashId);testWindow();}function testWindow(){var size=getWindowParams();flashTarget.style.width=(size.w>minWidth)?'100%':minWidth+'px';flashTarget.style.height=(size.h>minHeight)?'100%':minHeight+'px';var _timer=setTimeout('testWindow()',100);}function getWindowParams(){var _obj=new Object();_obj.w=pageWidth();_obj.h=pageHeight();return _obj;}function pageWidth(){return window.innerWidth!=null?window.innerWidth:document.documentElement&&document.documentElement.clientWidth?document.documentElement.clientWidth:document.body!=null?document.body.clientWidth:null;}function pageHeight(){return window.innerHeight!=null?window.innerHeight:document.documentElement&&document.documentElement.clientHeight?document.documentElement.clientHeight:document.body!=null?document.body.clientHeight:null;}function posLeft(){return typeof window.pageXOffset!='undefined'?window.pageXOffset:document.documentElement&&document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft?document.body.scrollLeft:0;}function posTop(){return typeof window.pageYOffset!='undefined'?window.pageYOffset:document.documentElement&&document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop?document.body.scrollTop:0;}function posRight(){return posLeft()+pageWidth();}function posBottom(){return posTop()+pageHeight();}";
		static private const JS_METHOD :String = "fitFlash";
		static private var _isInitialised :Boolean;
		private const INSTANTIATION_ERROR :String = "Don't instantiate com.spikything.utils.FlashSize directly. Just call FlashSize.setup(minWidth, minHeight);";
		
		public function FlashSize() 
		{
			throw new IllegalOperationError(INSTANTIATION_ERROR);
		}
		
		public static function setup(minWidth:uint, minHeight:uint):void
		{
			if (!_isInitialised) {
				_isInitialised = true;
				if (ExternalInterface.available) 
					ExternalInterface.call("eval", JAVASCRIPT);
			}
			if (ExternalInterface.available) 
				ExternalInterface.call(JS_METHOD, ExternalInterface.objectID, minWidth, minHeight);
		}
		
	}
	
}